//
//  EProcessBar.m
//  ProcessBarDemo
//
//  Created by Jaedong n.Die_ on 12-8-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "EProcessBar.h"
#import "Contants.h"

@interface EProcessBar (Private)

- (void)processBarAnimation;

@end

@implementation EProcessBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithColors:(NSArray *)colors {

    self = [super init];
    if(self) {
    
        self.backgroundColor = [UIColor whiteColor];
        
        _mutableColor = [[NSMutableArray alloc] initWithCapacity:2];
        [_mutableColor addObjectsFromArray:colors];
    }
    
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGFloat redColor;
    CGFloat greenColor;
    CGFloat blueColor;
    
    int nCount = self.frame.size.width / kColorFrameWidth + 1;
    int nColorCount = _mutableColor.count;

    for (int i = -nColorCount; i < nCount; i++) {
        
        CGColorRef colorRef = ((UIColor *)[_mutableColor objectAtIndex:fmod(abs(i), nColorCount)]).CGColor;
        const CGFloat *colorComponents = CGColorGetComponents(colorRef);
        if (CGColorGetNumberOfComponents(colorRef) == 2) {
            
            redColor = greenColor = blueColor = colorComponents[0];
        }
        else if (CGColorGetNumberOfComponents(colorRef) == 4) {
            
            redColor = colorComponents[0];
            greenColor = colorComponents[1];
            blueColor = colorComponents[2];
        }
        
        CGContextSetRGBFillColor(ctx, redColor, greenColor, blueColor, 1.0f);
        CGContextSetRGBStrokeColor(ctx, redColor, greenColor, blueColor, 1.0f);
        
        CGContextMoveToPoint(ctx, i * kColorFrameWidth + _xOffset , self.frame.size.height);
        CGContextAddLineToPoint(ctx, (i + 1) * kColorFrameWidth + _xOffset, self.frame.size.height);
        CGContextAddLineToPoint(ctx, i * kColorFrameWidth + _xOffset, 0);
        CGContextAddLineToPoint(ctx, (i - 1) * kColorFrameWidth + _xOffset, 0);
        CGContextAddLineToPoint(ctx, i * kColorFrameWidth + _xOffset, self.frame.size.height);
        CGContextClosePath(ctx);
        CGContextDrawPath(ctx, kCGPathFillStroke);
    }
    
    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 1.0, 1.0, 0.35, 
        1.0, 1.0, 1.0, 0.1 };
    
    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
    
    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGContextDrawLinearGradient(ctx, glossGradient, topCenter, midCenter, 0);
    
    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace); 
}

- (void)dealloc {

    [_mutableColor release];
    [_timer release];
    
    _mutableColor = nil;
    _timer = nil;
    
    [super dealloc];
}

#pragma mark - 
#pragma mark Instance Methods.

- (void)start {

    _timer = [NSTimer timerWithTimeInterval:kTimerInterval target:self selector:@selector(processBarAnimation) userInfo:nil repeats:YES];;
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)stop {

    if(_timer != nil && _timer.isValid) {
        
        [_timer invalidate];
        _timer = nil;
    }
}

#pragma mark -
#pragma mark Private Methods.

- (void)processBarAnimation {

    ++_xOffset;
    if(_xOffset >= (kColorFrameWidth * _mutableColor.count)) {
    
        _xOffset = 0;
    }
    
    [self setNeedsDisplay];
}
              
@end
